/**
 *  Model1
 *  Author: hakute
 *  Description: 
 */

model Model1

global {
	/** Insert the global definitions, variables and actions here */
	file shape_file_bounds <- file('../includes/Batiment/Bounds.shp');
	file shape_file_obtacles <- file('../includes/Batiment/Obtacles.shp');
	file shape_file_areas <- file('../includes/Batiment/Area.shp');
	file shape_file_safe_areas <- file('../includes/Batiment/Save_place.shp');
	file shape_file_fired_areas <- file('../includes/Batiment/Fired_Areas.shp');
	file shape_file_sonneur <- file('../includes/Batiment/Sonneur.shp');
	file shape_file_path <- file('../includes/Batiment/Path.shp');
	list list_areas of:areas;
	list list_obtacles of:obtacles;
	list list_path of:path_emergent;
	bool sonneur ;
	int sonneur_fois <-0;
	string path_fire <-'../images/fire.png';
	string path_smoke<-'../images/smoke.png';
	
	init {
		create obtacles from: shape_file_obtacles ;
		create areas from: shape_file_areas ;
		create safe_areas from: shape_file_safe_areas;
		create sonneur_normal from: shape_file_sonneur;
		create path_emergent from: shape_file_path;
		set list_areas <-areas as list;
		set list_obtacles <-obtacles as list;
		set sonneur <-false;
		create evacuees_normal number:20{
			set location <- any_point_in (one_of (areas));
		}
		create feux_potentiel number: 10{
			set location <- any_point_in (one_of (list_obtacles));
		}
	}
}

environment bounds: shape_file_bounds torus:false;

entities {
	/** Insert here the definition of the species of agents */
	
	//office
	species obtacles {
		string type; 
		rgb color <- rgb([155,0,0])  ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	species areas  {
		rgb color <- rgb('white') ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	species safe_areas {
		rgb color <- rgb('white') ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	species path_emergent {
		rgb color <- rgb('white') ;
		aspect base {
			draw geometry: shape color: color ;
		}
	}
	
	
	//evacuees
	species evacuees_normal skills : [ moving, situated ] control : fsm{
		float taile <-rnd(30.0)+15.0;
		rgb color <-rgb([rnd(255),rnd(255),rnd(255)]);
		int puissance <-2000;
		int niveauEffectue <-rnd(3)+1;
		float vitesseMax<-50.0;
		float vitesseActuelle <-30.0;
		int direction <-180;
		float range_voir<-100.0;
		safe_areas but <-nil;
		agent danger <- nil;
		
		reflex move_normal when: (danger = nil){
			do action: move_normal;	
			let voir_feux <- ( self neighbours_at ( range_voir ) of_species feux ) ;
			ask voir_feux{
				set myself.danger <- self;
			}
			let voir_fumme <- ( self neighbours_at ( range_voir ) of_species fumme ) ;
			ask voir_feux{
				set myself.danger <- self;
			}
		}
		
		reflex emergent when: ((danger != nil) or (sonneur = true)) {
			do action: emergent;
		}
		
		action move_normal {
			do wander speed: vitesseActuelle  amplitude: direction bounds: list_areas ;
		}
		
		action emergent{
			create evacuees_emergent{
				set self . color <- myself.color ;
				set self . taile <- myself . taile ;
				set self . location <- myself . location ;
			}
			do action: die;
		}
		
		aspect base {
			draw shape:circle size:taile color: color ;
			draw shape: circle color: rgb('orange') size: range_voir*2+taile empty: true;
			//draw shape: circle color: rgb('green') size: range_entendre*2+taile empty: true;
		}		
}

species evacuees_emergent skills : [ moving, situated ] control : fsm{
		float taile <-rnd(30.0)+15.0;
		rgb color <-rgb([0,rnd(200)+55,0]);
		int puissance <-200;
		int niveauEffectue <-rnd(3)+1;
		float vitesseMax<-50.0;
		float vitesseActuelle <-30.0;
		int direction <-180;
		float range_voir<-100.0;
		safe_areas but <-nil;
		
		reflex decrement_puissance{
			if ((niveauEffectue < 2)and(taile<20.0)){
				set puissance <-puissance -3;
				}
			if ((niveauEffectue =3)and(taile<20.0)){
				set puissance <-puissance -2;
			}
			if ((niveauEffectue =3)and(taile>=20.0)){
				set puissance <-puissance -1;
			}
			if (puissance =0){
				do action: dead;
			}
		}
		action dead{
			create evacuees_mort{
				set self . color <- [ 0,0,0 ] as rgb ;
				set self . taile <- myself . taile ;
				set self . location <- myself . location ;
			}
			do action: die;
		}
		reflex find_path when: but = nil{
			do action: moveNormal;
			let areas_emergents_voir <- ( (self neighbours_at (range_voir)) of_species safe_areas );
			ask areas_emergents_voir {													
				set myself.but <- self;
			}			
		}
		
		action moveNormal {
			do wander speed: vitesseMax  amplitude: direction bounds: list_areas ;
		}
		
		reflex gotoEmergentAreas when: ( but != nil ) {			
				do action: gotoEmergentAreas;																					
				}		
			
		action gotoEmergentAreas{
 			if ( but != nil ) {
 				do goto target: but.location speed: vitesseMax;
 				do action: die;
 				}	
		}
		
		aspect base {
			draw shape:circle size:taile color: color ;
			draw shape: circle color: rgb('red') size: range_voir*2+taile empty: true;
		}
	}

species evacuees_mort skills : [ moving, situated ] control : fsm{
		float taile <-30.0;
		rgb color <-rgb([0,0,0]);
		
		aspect base {
			draw shape:circle size:taile color: color ;
		}
	}
	
	//feux
	species feux_potentiel skills : [ situated , visible ] control : fsm{
		rgb color<- rgb('white');
		float taile <-float(rnd(10.0));
		float rayon_effectuer <-5.0;
		float vitesse_propagation <-5.0;
		int vitesse_creer <-2;
	
	reflex burning{
		if flip(0.01){
			do action: burning;
		}
	}
	action burning{
		create feux{
				set self . color <- [ 255,rnd(200)+55,0 ] as rgb ;
				set self . taile <- myself . taile ;
				set self . location <- myself . location ;
			}
		do action: die;
	}
	
	aspect base {
			draw shape:circle size:taile color: color ;
			}
		}
	
	species feux skills : [ situated , visible ] control : fsm{
		
		rgb color<- rgb([255, rnd (200)+55,0]);
		float taile <-float(rnd(10.0));
		float rayon_effectuer <-5.0;
		float vitesse_propagation <-0.02;
		int vitesse_creer <-1;
		int duree <-0;
		
		
	reflex enlargir{
		set duree <- duree+1;
		if (taile < 100){
			set taile <- taile + (vitesse_propagation * duree);
			}
		else {
			set taile <- taile - 10.0;
			if (taile =0){
				do action:die;
			}
		}
		if (duree<5){
			create fumme number: vitesse_creer{
				set self . color <- 'black' as rgb ;
				set self . taile <- myself.taile*50;
				set self . location <- myself . location ;
				}
		}
	}
	
	reflex creer_feux{
		if (duree <2){
			let agent_list <-( self neighbours_at ( rayon_effectuer ) of_species obtacles );
			ask agent_list{
				create feux_potentiel number:2 {
					set self . color <- [ 255,rnd(200)+55,0 ] as rgb ;
					set self . taile <- 10.0;
					set self . location <-  any_point_in (one_of (list_obtacles)) ;
				}
			}	
		}
	}
		
	
	reflex propager{
		let voisin <- ( self neighbours_at ( rayon_effectuer ) of_species feux_potentiel ) ;
		if ( flip ( 0.01 ) ) {
				ask voisin {
					do action: burning ;
				}
			}
	}
	
	reflex kill{
		let evacuees_mourrir <- ( self neighbours_at ( rayon_effectuer ) of_species evacuees_emergent ) ;
		ask evacuees_mourrir{
			do action:dead;
		}
	}
	
	aspect base {
			draw shape:circle size:taile color: color ;
			}
	aspect image {
			draw image : path_fire size : taile ;
			}
	}
	
	
	//fumme
	species fumme skills : [ moving , visible ] control : fsm{
		rgb color<- rgb('black');
		float taile <-10.0;
		float vitesse_deplacer <-10.0;
		int duree <-0;
		
		reflex deplacer{
			do action: deplacer;
			set duree <- duree+1;
			if (duree>200){
				do action:die;
			}
			
		}
	
		action deplacer{
			do action:wander speed:vitesse_deplacer amplitude:(180+rnd(90));
		}
		aspect base {
			draw shape:circle size:taile color: color ;
			}
		aspect image {
			draw image : path_smoke size : taile ;
			}
		}
	
	// sonneur	
	species sonneur_normal skills : [ situated , visible ] control : fsm{
		rgb color<- rgb('blue');
		float rayon_detection <-100.0;
		int duree <-500;
		agent danger <- nil;
	
		reflex normal when: (danger = nil){
		let danger_list <- ( (self neighbours_at (rayon_detection)) of_species fumme );
		ask danger_list {													
				set myself.danger <- self;
			}	
		}
	
		reflex active when: ((danger!=nil)and(sonneur_fois !=1)){
			set sonneur_fois <- 1;
			let sonneur_list <- list(sonneur_normal);
			set sonneur <-true;
			ask sonneur_list {
				do action: active;
			}
		}
		action active{
			create sonneur_active from: shape_file_sonneur number:1{
				set self . color <- 'red' as rgb ;
				set self . location <- myself . location ;
				}
			do action:die;
		}
		
	
		aspect base {
			draw geometry: shape color: color ;
			}
		}
		
	species sonneur_active skills : [ situated , visible ] control : fsm{
		rgb color;
		float rayon_detection ;
		int duree_sonneur <-500;
		
		reflex deactive{
			set duree_sonneur <- duree_sonneur -1;
			if (duree_sonneur=0){
				do action: normal;
			}
		}
		action normal{
			create sonneur_normal from: shape_file_sonneur number:1 {
				set self . color <- 'white' as rgb ;
				set self . location <- myself . location ;
				}
			do action: die;
		}
		aspect base {
			draw geometry: shape color: color ;
			}
		}
	
}

experiment Model1 type: gui {
	/** Insert here the definition of the input and output of the model */
	output {
		inspect name: 'Agent' type : agent ; 
		inspect name: 'Species' type : species ;
		display super_marcher refresh_every: 1 {
			species obtacles aspect: base ;
			species evacuees_normal aspect: base;
			species evacuees_emergent aspect: base;
			species evacuees_mort aspect: base;
			//species feux_potentiel aspect: base;
			species feux aspect: image;
			species fumme aspect: image;
			species sonneur_normal aspect: base;
			species sonneur_active aspect: base;
		}
	}
}